package cn.stylefeng.guns.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.goods.entity.GoodData;
import cn.stylefeng.guns.goods.entity.Goods;
import cn.stylefeng.guns.goods.dto.GoodsDTO;
import cn.stylefeng.guns.goods.mapper.GoodsMapper;
import cn.stylefeng.guns.goods.service.GoodsService;
import cn.stylefeng.guns.goods.enums.GoodsExceptionEnum;
import cn.stylefeng.guns.goodsdetail.entity.GoodsDetail;
import cn.stylefeng.guns.goodsdetail.service.GoodsDetailService;
import cn.stylefeng.guns.user.entity.User;
import cn.stylefeng.guns.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * goods 服务实现类
 *
 * @author aria
 * @date 2021/02/20 20:24
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsDetailService goodsDetailService;
    @Autowired
    private UserService userService;

    @Override
    public PageResult<Goods> page(GoodsDTO goodsDTO) {

        // 构造条件
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        //审核通过
        if (goodsDTO.getAudit() == 1) {
            queryWrapper.eq(Goods::getAudit, goodsDTO.getAudit());
        }
        // 拼接查询条件-
        if (goodsDTO != null && ObjectUtil.isNotEmpty(goodsDTO.getGoodsId())) {
            queryWrapper.like(Goods::getGoodsId, goodsDTO.getGoodsId());
        }

        // 拼接查询条件-
        if (goodsDTO != null && ObjectUtil.isNotEmpty(goodsDTO.getUserId())) {
            queryWrapper.like(Goods::getUserId, goodsDTO.getUserId());
        }
        // 查询分页结果
        PageResult<Goods> goodsPageResult = new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
        List<Goods> rows = goodsPageResult.getRows();
        for (int i = 0; i < rows.size(); i++) {
            Goods goods = rows.get(i);
            goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        }
        return goodsPageResult;
    }

    @Override
    public List<Goods> list(GoodsDTO goodsDTO) {
        Goods goods = null;

        // 构造条件
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getAudit, goodsDTO.getAudit());
        List<Goods> list = this.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            goods = list.get(i);
            goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseData add(GoodsDTO goodsDTO) {

        //查找上传用户信息
        Integer userId = goodsDTO.getUserId();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        User user = userService.getBaseMapper().selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(user)) {
            return new ErrorResponseData(400, "该用户不存在");
        }

        //设置默认值
        //库存
        if (ObjectUtil.isEmpty(goodsDTO.getGoodsCount())) {
            goodsDTO.setGoodsCount(100);
        }
        //销量
        if (ObjectUtil.isEmpty(goodsDTO.getGoodsVolume())) {
            goodsDTO.setGoodsVolume(0);
        }
        //材质
        if (ObjectUtil.isEmpty(goodsDTO.getQuality())) {
            goodsDTO.setQuality("未知");
        }
        //尺寸
        if (ObjectUtil.isEmpty(goodsDTO.getSize())) {
            goodsDTO.setSize("未知");
        }
        //颜色
        if (ObjectUtil.isEmpty(goodsDTO.getColor())) {
            goodsDTO.setColor("未知");
        }
        //结构描述
        if (ObjectUtil.isEmpty(goodsDTO.getDescribe())) {
            goodsDTO.setDescribe("未知");
        }
        //精选判断
        if (ObjectUtil.isEmpty(goodsDTO.getChoiceness())) {
            goodsDTO.setChoiceness(0);
        }
        //
        if (ObjectUtil.isEmpty(goodsDTO.getAudit())) {
            goodsDTO.setAudit(1);
        }
        // 将dto转为实体
        Goods goods = new Goods();
        BeanUtil.copyProperties(goodsDTO, goods);
        //添加商品
        this.baseMapper.insert(goods);
        //获取自增id
        Integer goodsId = goods.getGoodsId();
        //添加详情图片
        List<GoodsDetail> detailImage = goodsDTO.getDetailImage();
        if (detailImage != null) {
            for (GoodsDetail goodsDetail : detailImage) {
                //设置外键id
                goodsDetail.setGoodsId(goodsId);
                goodsDetailService.insert(goodsDetail);
            }
        }

        return new SuccessResponseData();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(GoodsDTO goodsDTO) {
        this.removeById(goodsDTO.getGoodsId());
    }

    @Override
    public void edit(GoodsDTO goodsDTO) {

        Goods g = new Goods();
        g.setGoodsId(goodsDTO.getGoodsId());

        // 根据id查询实体
        Goods goods = this.queryGoods(g);

        // 请求参数转化为实体
        BeanUtil.copyProperties(goodsDTO, goods);

        this.updateById(goods);
    }

    //根据id查详情
    @Override
    public Goods detail(GoodsDTO goodsDTO) {
        // 构造
        Goods goods = new Goods();
        goods.setGoodsId(goodsDTO.getGoodsId());
        Goods goods1 = this.queryGoods(goods);
        //设置多图
        goods1.setDetailImage(goodsDetailService.getListByGoodsId(goods1));
        return goods1;
    }

    //精选
    @Override
    public Object popList(GoodsDTO goodsDTO) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("choiceness", 1);
        queryWrapper.eq("audit", 1);
        List<Goods> list = this.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            Goods goods = list.get(i);
            goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        }
        return list;
    }

    //热销
    @Override
    public Object sellList(GoodsDTO goodsDTO) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit", 1);
        queryWrapper.orderByDesc("goods_volume");
        List<Goods> list = this.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            Goods goods = list.get(i);
            goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        }
        return list;
    }

    //新款
    @Override
    public Object newList(GoodsDTO goodsDTO) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit", 1);
        queryWrapper.orderByDesc("goods_id");
        List<Goods> list = this.list(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            Goods goods = list.get(i);
            goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        }
        return list;
    }

    //分类查询
    @Override
    public Object listByType(GoodsDTO goodsDTO) {
        // 查询分页结果
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", goodsDTO.getType());
        queryWrapper.eq("audit", 1);
        PageResult<Goods> goodsPageResult = new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
        List<Goods> rows = goodsPageResult.getRows();
        for (int i = 0; i < rows.size(); i++) {
            Goods goods = rows.get(i);
            goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        }
        return goodsPageResult;
    }

    //根据用户编号查询
    @Override
    public Object listByUserId(GoodsDTO goodsDTO) {
        // 查询分页结果
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", goodsDTO.getUserId());
        PageResult<Goods> goodsPageResult = new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
        List<Goods> rows = goodsPageResult.getRows();
        for (int i = 0; i < rows.size(); i++) {
            Goods goods = rows.get(i);
            goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        }
        return goodsPageResult;
    }

    //根据商品名模糊查询
    @Override
    public Object selectLikeName(GoodsDTO goodsDTO) {
        if (ObjectUtil.isEmpty(goodsDTO.getGoodsName())) {
            return list();
        }
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("goods_name", goodsDTO.getGoodsName());
        queryWrapper.eq("audit", 1);
        List<Goods> list = baseMapper.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            Goods goods = list.get(i);
            goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        }
        return list;
    }

    @Override
    public List<GoodData> data() {
        List<GoodData> data = goodsMapper.selectData();
        for (GoodData good: data) {
            if ("0".equals(good.getName()))
                good.setName("女装");
            if ("1".equals(good.getName()))
                good.setName("男装");
            if ("2".equals(good.getName()))
                good.setName("包袋");
            if ("3".equals(good.getName()))
                good.setName("配饰");
            if ("4".equals(good.getName()))
                good.setName("五金");
            if ("5".equals(good.getName()))
                good.setName("玩具");
        }

        return data;
    }


    /**
     * 获取goods
     *
     * @author aria
     * @since 2021/02/20 20:24
     */
    private Goods queryGoods(Goods goodsDTO) {
        Goods goods = this.getById(goodsDTO.getGoodsId());
        if (ObjectUtil.isEmpty(goods)) {
            throw new ServiceException(GoodsExceptionEnum.NOT_EXISTED);
        }
        //设置详情图片集合
        goods.setDetailImage(goodsDetailService.getListByGoodsId(goods));
        return goods;
    }
}